import Promise from '../../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../../utils/regenerator-runtime/runtime'
const utils = require('../../../../utils/util.js');
const app=getApp();
Page({
  data: {
    url:"/api/Task/getTaskDetail",
    changeStatusUrl:"/api/Task/changeStatus",
    info:null,
    taskid:null,
    token:null
  },
  changeType: async function(e){
    var params={};
    params.status = e.currentTarget.dataset.type;
    params.taskid = this.data.taskid;
    params.token = this.data.token;
    var result=await this._changeStatusRequset(params);
    if(result.state){
      var msg=result.msg;
      wx.showModal({
        title:msg,
        showCancel:false,
        complete: data=>{
          wx.navigateBack({
            delta: 1
          })
          //this.onLoad({taskid:this.data.taskid});
        }
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
  },
   onLoad: async function (params) {
     console.log("onLoad");
    var taskid=params.taskid;
    this.setData({
      taskid:taskid
    })
    const token = await app.getToken();
    this.setData({
      token:token
    })
    var result=await this._loadData();
    if(result.state){
      var info=result.info;
      this.setData({
        info:info
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
  },
  _loadData:function(){
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        app.request(this.data.url,{token:this.data.token,taskid:this.data.taskid},res=>{
          //console.log(res);
          console.log("res");
          console.log(res);
          var data=res.data;
          // var data={
          //   state:true,
          //   data:
          //   {
          //     "headimg":"/images/card-on.png",
          //     "nickname":"撒大大的独家账号",
          //     "username":"张珊珊",
          //     "userphone":"13765843421",
          //     "remark":"遇到什么什么情况，将要怎样怎样；",
          //     "status":"1"
          //   }
          // };
          debugger;
          if(data.state){
            //resolve({state:true,info:JSON.parse(data.info)});
            resolve({state:true,info:data.data});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
        },error=>{
          resolve({state:false,msg:"刷新失败，请重试"});
        })
    });
    return p;
  },
  _changeStatusRequset:function(params){
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        console.log(params);
        app.request(this.data.changeStatusUrl,params,res=>{
          console.log("res");
          console.log(res);
          var data=res.data;
          var data={
            state:true,
            msg:"提交成功"
          };
          if(data.state){
            //resolve({state:true,info:JSON.parse(data.info)});
            resolve({state:true,msg:data.msg});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
        },error=>{
          resolve({state:false,msg:"刷新失败，请重试"});
        })
    });
    return p;
  }
})